<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h2>用户登录</h2>
        <p>请输入您的用户名登录系统</p>
      </div>

      <el-form ref="form" :model="stu" label-width="0px" class="login-form">
        <el-form-item>
          <el-input
              v-model="stu.sname"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              clearable
              @keyup.enter.native="onSubmit"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="stu.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
              @keyup.enter.native="onSubmit"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              @click="onSubmit"
              class="login-button"
              :loading="loading"
              round
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "View1Login",
  data() {
    return {
      stu: {
        sname: '',
        password:''
      },
      loading: false
    }
  },
  methods: {
    onSubmit() {
      if (!this.stu.sname.trim()) {
        this.$message({
          message: "请输入用户名",
          type: "warning",
          duration: 1000
        });
        return;
      }

      if (!this.stu.password.trim()) {
        this.$message({
          message: "请输入密码",
          type: "warning",
          duration: 1000
        });
        return;
      }
      this.loading = true;

      // 使用 JSON 格式发送数据
      axios.post('/login', this.stu, {
        headers: {
          'Content-Type': 'application/json'
        }
      }).then(ret => {
        if (ret.code == 2000) {
          this.$message({
            message: "登录成功",
            type: "success",
            duration: 1000
          })
          //存储用户名
          this.$store.commit('setUsername',ret.data.sname)
          // 登录成功 跳转主页面
          this.$router.push("/admin")
        } else {
          this.$message({
            message: "登录失败",
            type: "error",
            duration: 1000
          });
        }
      }).catch(() => {
        this.$message({
          message: "网络错误，请稍后重试",
          type: "error",
          duration: 1000
        });
      }).finally(() => {
        this.loading = false;
      });
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 40px 30px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  animation: slideUp 0.5s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  font-size: 24px;
  margin-bottom: 10px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
  height: 45px;
  margin-top: 20px;
  font-size: 16px;
  background: linear-gradient(to right, #667eea, #764ba2);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

::v-deep .el-input__inner {
  height: 45px;
  border-radius: 25px;
  padding-left: 20px;
  border: 1px solid #ddd;
  transition: all 0.3s ease;
}

::v-deep .el-input__inner:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

::v-deep .el-form-item {
  margin-bottom: 20px;
}

@media (max-width: 500px) {
  .login-card {
    padding: 30px 20px;
  }
}
</style>
